const query = require("../db/query");
// 管理员所有文章
const articlecha = async (ctx) => {
  const sql = `SELECT homeall.id, article.title1, article.content1, article.reading, article.upvote, article.time1
FROM article
JOIN homeall ON article.homeallid = homeall.id;
`;
  return await query(sql);
};
// -- 医生登录后查看自己的文章  -- 454435983561005800
const doctorarticle = async (ctx) => {
  const sql = `
SELECT homeall.id,homeall.identitycard,homeall.name,article.id, article.title1, article.content1, article.reading, article.upvote, article.time1
FROM article 
JOIN homeall ON article.homeallid = homeall.id
  WHERE homeall.identitycard="${ctx.identitycard}";
`;
  return await query(sql);
};

// --新增文章
const doctoraddarticle = async (ctx) => {
  // 获取到当前时间
  function formatDate(date) {
    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, "0");
    const day = String(date.getDate()).padStart(2, "0");
    const hour = String(date.getHours()).padStart(2, "0");
    const minute = String(date.getMinutes()).padStart(2, "0");
    const second = String(date.getSeconds()).padStart(2, "0");
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
  }
  const currentDate = new Date();
  const formattedTime = formatDate(currentDate);
  console.log(formattedTime);
  // 输出示例：2024-12-02 18:05:37
  const sql = `	INSERT INTO article (homeallid, title1, content1, reading, upvote, time1)
VALUES ('${ctx.homeallid}', '${ctx.title1}', '${ctx.content1}', 0, 0, ' ${formattedTime}');
`;
  return await query(sql);
};
// 删除文章 
const doctordeletearticle = async (ctx) => {
  const sql = `DELETE FROM article WHERE id=${ctx.id}`;
  return await query(sql);
};  
module.exports = { articlecha, doctorarticle, doctoraddarticle ,doctordeletearticle};
